package tech.aistar.day12.exception;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 本类用来演示: 异常
 *
 * @author: success
 * @date: 2021/3/23 2:12 下午
 */
public class ExceptionDemo {
    public static void main(String[] args) {
        //System.out.println(10/0);//ArithmeticException

        List<String> list = new ArrayList<>();

        //System.out.println(list.get(100));

//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        //处理非运行时异常
//        try+多个catch块

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        try {
            //注意点
            //1. 如果try块中某行出现了异常,那么该try{}中的异常行下方的代码将不会执行了...
            //2. 如果没有发生任何异常,catch块中的代码是不会执行的
            Date date = sdf.parse("2010");
            InputStream in = new FileInputStream("src/tech/aistar/day12/Students.java");
            System.out.println("code..");
        } catch (ParseException e) {
            System.out.println("ParseException...");
            //e.printStackTrace();
        } catch (FileNotFoundException e) {
            //e.printStackTrace();
            System.out.println("FileNotFoundException...");
        }
        System.out.println("final....");

        System.out.println("====");


        //jdk7.x try+catch(异常类型1 | 异常类型2 | 异常类型3)
        //针对所有的异常进行统一的处理...
        try {
            Date date = sdf.parse("2010");
            InputStream in = new FileInputStream("src/tech/aistar/day12/Student.java");
        } catch (ParseException | FileNotFoundException e) {
            e.printStackTrace();
        }

        //总的父类来统一进行处理
        try {
            Date date = sdf.parse("2010");
            InputStream in = new FileInputStream("src/tech/aistar/day12/Student.java");
        } catch (Exception e) {//异常的总的父类
            e.printStackTrace();
        }

        System.out.println("===笔试 - 多个catch注意点==");
        try {
            Date date = sdf.parse("2010");
        } catch (ParseException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

        System.out.println("===最终的版本==");

        try {
            InputStream in = new FileInputStream("src/tech/aistar/day12/Student.java");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            //finally中一般适合进行资源的释放,资源的关闭等操作.
            System.out.println("无论是否出现异常,都会执行...");
        }

        System.out.println("===笔试题===");

        try{
            InputStream in = new FileInputStream("src/tech/aistar/day12/Student.java");
            System.out.println(in);
            return;//return语句在finally后面执行..
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            System.out.println("finally...");
        }
    }
}
